package com.feng.fengDUtil.dao;

import java.util.List;
import java.util.stream.Collectors;

public class SqlUtil {

  public static String geneUpdateSql(String table, String id, List<FieldInfo> columns) {
    StringBuilder sb = new StringBuilder("update ");
    sb.append(table).append(" set ").append(joinColumn(columns, ",")).append(updateWhere(id));
    return sb.toString();
  }

  public static String geneBatchUpdateSqlInner(String tableName, List<FieldInfo> allFields) {
    StringBuilder sb = new StringBuilder("select");
    sb.append(allFields.stream().map(n -> " ? as ".concat(n.getColumnName())).collect(Collectors.joining(",")));
    sb.append(" from ").append(tableName);
    return sb.toString();
  }

  public static String geneBatchUpdateSqlOuter(String tableName, List<FieldInfo> keys, List<FieldInfo> fieldList) {
    String ids = keys.stream().map(n -> n.getColumnName()).collect(Collectors.joining(","));
    StringBuilder sb = new StringBuilder("update ").append(tableName).append(" u join (%s) b using(").append(ids).append(") set ");
    String sets = fieldList.stream().map(n -> n.getColumnName()).map(m -> "u.".concat(m).concat(" = b.").concat(m)).collect(Collectors.joining(","));
    sb.append(sets);
    return sb.toString();
  }

  private static String getQuestionMark(FieldInfo fieldInfo) {
    if (fieldInfo.getGeometry() != null) {
      String geomType = fieldInfo.getGeometry().value();
      switch (geomType) {
        case "point":
          return "st_pointFromText(?)";
        case "polygon":
          return "st_polygonFromText(?)";
        default:
          return "ST_GeomFromText(?)";
      }
    }
    return "?";
  }

  private static String joinColumn(List<FieldInfo> fieldList, String separator) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < fieldList.size(); i++) {
      sb.append(fieldList.get(i).getColumnName()).append("=")
          .append(getQuestionMark(fieldList.get(i)));
      if (i < fieldList.size() - 1) {
        sb.append(separator);
      }
    }
    return sb.toString();
  }

  public static String geneSelectPart(List<FieldInfo> fieldList) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < fieldList.size(); i++) {
      FieldInfo field = fieldList.get(i);
      if (field.getGeometry() != null) {
        sb.append("st_asText(").append(field.getColumnName()).append(") as ")
            .append(field.getColumnName());
      } else {
        sb.append(field.getColumnName());
      }
      if (i < fieldList.size() - 1) {
        sb.append(",");
      }
    }
    return sb.toString();
  }


  private static String updateWhere(String id) {
    return new StringBuilder(" where ").append(id).append(" = ? ").toString();
  }

  public static String geneUpdateSqlPk(String table, List<FieldInfo> pkfields,
      List<FieldInfo> otherFields) {
    StringBuilder sb = new StringBuilder("update ");
    sb.append(table).append(" set ").append(joinColumn(otherFields, ",")).append(" where ")
        .append(joinColumn(pkfields, " and "));
    return sb.toString();
  }

  public static String geneDelSqlByWhere(String table, List<FieldInfo> columns) {
    StringBuilder sb = new StringBuilder("delete from  ");
    sb.append(table).append(" where ").append(joinColumn(columns, " and "));
    return sb.toString();
  }

  private static String joinFieldNames(List<FieldInfo> fieldList) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0, size = fieldList.size(); i < size; i++) {
      sb.append(fieldList.get(i).getColumnName());
      if (i < size - 1) {
        sb.append(",");
      }
    }
    return sb.toString();
  }

  public static String geneInsertSql(String table, List<FieldInfo> fieldList) {
    StringBuilder sb = new StringBuilder(table);
    sb.append(" ( ").append(joinFieldNames(fieldList)).append(" ) values ( ");
    for (int i = 0, size = fieldList.size(); i < size; i++) {
      sb.append(getQuestionMark(fieldList.get(i)));
      if (i < size - 1) {
        sb.append(",");
      }
    }
    sb.append(")");
    return sb.toString();
  }

  public static String geneSelectByPkSql(String baseSelectSql, List<FieldInfo> pkfields) {
    StringBuilder sb = new StringBuilder(baseSelectSql)
        .append(" where ").append(joinColumn(pkfields, " and "));
    return sb.toString();
  }


}
